<template>
  <div id="home">
    <!-- 导航栏 -->
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>

    <!-- 选项卡 -->
    <tab-control 
      @tabClick="tabClick" 
      ref="tabControl1"
      class="tab-control"
      v-show="isTabFixed"
      />

    <scroll 
      class="content" 
      ref="scroll" 
      :probe-type="3" 
      @scroll="contentScroll"
      :pull-up-load="true"
      @pullingUp="loadMore">
      <!-- 轮播图 -->
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
      <!-- 信息展示 -->
      <recommend-view :recommends="recommends"></recommend-view>
      <!-- 潮流推荐 -->
      <feature-view/>
      <!-- 选项卡 -->
      <tab-control @tabClick="tabClick" ref="tabControl2"/>
      <!-- 商品信息展示 -->
      <goods-list :goods="showGoodsList"></goods-list>
    </scroll>
    <!-- 返回顶部组件 .nativ 监听组件的原生事件时使用-->
    <back-top @click.native="backTop" v-show="isShowBackTop"/>
  </div>
</template>

<script>

// 导入轮播图
import HomeSwiper from './childComps/HomeSwiper.vue'
// 导入 信息展示
import RecommendView from './childComps/RecommendView.vue'
// 导入 潮流推荐
import FeatureView from './childComps/FeatureView.vue'
// 导入商品信息展示
import GoodsList from '../../components/content/goods/GoodsList'

// 导入 导航栏组件
import NavBar from 'components/common/navbar/NavBar'
// 导入 选项卡
import TabControl from 'components/content/tabControl/TabControl'
// 导入 better-scroll (封装) 
import Scroll from 'components/common/scroll/Scroll'


// 导入 网络请求
import {getHomeMultidata, getHomeGoods} from 'network/home'

// 导入混入 
import {itenListenerMixin, BackTopMixin} from 'common/mixin.js'

export default {
  name: 'home',
  components:{
    HomeSwiper,
    RecommendView,
    FeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll
  },
  mixins: [itenListenerMixin, BackTopMixin],
  data(){
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {
          page: 0,
          list : []
        },
        'new': {
          page: 0,
          list : []
        },
        'sell': {
          page: 0,
          list : []
        }
      },
      currentType: 'pop',
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    }
  },
  // 组件插件完触发 created
  created(){
    // 1. 请求多个数据
    this.getgetHomeMultidata()
    
    // 请求Goods数据
    this.getHomeGoods('pop')

    this.getHomeGoods('new')

    this.getHomeGoods('sell')

  },
  mounted(){
  },
  watch:{},
  computed:{
    showGoodsList() {
      return this.goods[this.currentType].list
    }
  },
  methods:{
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    // 首页滚动监听
    contentScroll(postions) {
      // 判断是否需要显示 BackTop 
      this.listenShowBackTop(postions)

      // 2. 决定tabControl 是否吸顶
      if (this.tabOffsetTop < -postions.y) {
        this.isTabFixed = true
      }else {
        this.isTabFixed = false
      }
    },
    loadMore() {
      // 给当前的数据 上拉加载更多
      this.getHomeGoods(this.currentType)

      // 重新获取可滚动高度 
      this.$refs.scroll && this.$refs.scroll.refresh()
    },

    swiperImageLoad() {
      // 获取tabControl的offsetTop
      // 所有组件都有一个属性 $el: 获取组件中的元素
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },

    /* 
      网络请求相关方法
    */
    getgetHomeMultidata() {
      getHomeMultidata()
        .then(res => {
          // 保存数据
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
          // console.log('数据请求成功', this.banners)
        })
    },
    getHomeGoods(type) {
      // 获取页码
      const page = this.goods[type].page + 1
      getHomeGoods(type, page)
        .then(res => {
          const newList = res.data.list
          this.goods[type].list.push(...newList)
          this.goods[type].page += 1

          this.$refs.scroll.finishPullUp()
        }) 
    }
  },
  //进入活跃状态
  activated() {
    // 加载到上次离开的位置
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.refresh()
  },
  // 离开活跃状态
  deactivated() {
    // 记录页面当前位置
    this.saveY = this.$refs.scroll.getScrollY()

    // 取消全局事件
    this.$bus.$off('imgLoad', this.itemImgLoadFn)
  }
}
</script>
<style scoped>
#home {
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* 原生滚动 */
  /* position: fixed;
  top: 0px;
  left: 0px;
  right: 0px;
  z-index: 9; */
}

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0px;
  right: 0px;
}

.tab-control {
  position: relative;
  z-index: 9;
}
</style>